package com.yangcangclub.system.service.impl;

import com.yangcangclub.system.domain.ImgUpload;
import com.yangcangclub.system.service.SystemService;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.system.domain.AppProperties;
import com.yangcangclub.system.domain.ImgValidateCode;
import com.yangcangclub.system.domain.PushConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *系统服务业务
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    ImgValidateCode imgValidateCode;

    @Autowired
    ImgUpload imgUpload;

    @Autowired
    AppProperties appProperties;

    @Autowired
    PushConfig pushConfig;

    /**
     * 获取图片验证码
     */
    @Override
    public void getImgcode(HttpServletResponse response, String key,
                           int width, int height) throws Exception {

        if (key == null || response == null || width < 1 || height < 1) {
            throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        }

        imgValidateCode.getImgcode(response, key, width, height);

    }

    /**
     * 上传图片
     */
    @Override
    public String upload(byte[] data, String fileType) {
        return imgUpload.upload(data, fileType);
    }

    @Override
    public String getToken() {
        return imgUpload.getToken();
    }


    /**
     * 获取app系统配置
     */
    @Override
    public List<Map<String, Object>> getAppProperties(String platType) {
        List<Map<String, Object>> list = appProperties.queryProperList(platType);
        if (list != null) {
            Map<String, Object> temp;
            String str;
            for (int i = 0; i < list.size(); i++) {
                temp = list.get(i);
                str = (String) temp.get("value");
                temp.put("value_list", str.split(";"));
            }
        }

        return list;

    }


    /**
     * 获取用户个人推送配置列表
     */
    @Override
    public List<Map<String, Integer>> getUserPushConflist(int userId) {

        //只返回关闭的选项
        List<Map<String, Integer>> list=pushConfig.queryConflist(userId);
        if(list!=null){
            for(int i=0;i<list.size();i++){
                  if(list.get(i).get("state")==1){
                      list.remove(i);
                      i-=1;
                  }
            }
        }

        return pushConfig.queryConflist(userId);
    }


    /**
     * 用户修改个人推送配置
     */
    @Override
    public synchronized boolean  modifyPushConfig(int userId, int pushType, int state) {
        List<Map<String, Integer>> list=this.getUserPushConflist(userId);
        if(list!=null){
            for(Map<String, Integer> map:list){
                if(pushType==map.get("push_type")){
                    //修改
                   return  pushConfig.updatePushConf(userId,pushType,state);
                }
            }
        }

        //新增
       return  pushConfig.addPushConf(userId,pushType,state);
    }

    @Override
    public void addRequestErrorData(String request_url, String error_msg){
        appProperties.addRequestErrorData(request_url,error_msg);
    }

}
